package methodCallTreeGen;

import dslProgramGen.DslProgramGen;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import static dslProgramGen.util.DslProgramGenUtil.mainClassIndex;
import static utils.Utils.writeLineToDstFile;

public class GenMainiClasses {
    public static void readStaticFieldAndWriteToFile(String srcFilePath, String dstFilePath) throws FileNotFoundException {
        Scanner scanner = new Scanner(new FileReader(srcFilePath));
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            writeLineToDstFile(line, dstFilePath);
        }
        scanner.close();
    }
    public static void readStaticDslBlockAndWriteToFile(String srcFilePath, String dstFilePath) throws FileNotFoundException {
        Scanner scanner = new Scanner(new FileReader(srcFilePath));
        long methodDslStatementNum = 4000; // 一个静态函数里包含多少statement
        String methodName = "f";
        int methodIndex = 0;
        long lineIndex = 0;
        List<String> callStaticMethodList = new ArrayList<>();
        while (scanner.hasNextLine()) {
            if (lineIndex == methodDslStatementNum) {
                lineIndex = 0;
                methodIndex++;
                writeLineToDstFile("}", dstFilePath);
            }

            if (lineIndex == 0) {
                // 表示一个函数定义开始
                if (DslProgramGen.isGenJavaProgram) {
                    writeLineToDstFile("public static void " + methodName + methodIndex + "() {", dstFilePath);
                } else {
                    writeLineToDstFile("void " + methodName + methodIndex + "() {", dstFilePath);
                }

                callStaticMethodList.add(methodName + methodIndex + "()");
            }
            String line = scanner.nextLine();
            writeLineToDstFile(line, dstFilePath);
            lineIndex++;

        }
        // 最后一个f没有右括号
        writeLineToDstFile("}", dstFilePath);
        if (DslProgramGen.isGenJavaProgram) {
            writeLineToDstFile("static {", dstFilePath);
        } else {
            writeLineToDstFile("void " + methodName + "() {", dstFilePath);
        }

        for (String calleeExpr : callStaticMethodList) {
            writeLineToDstFile(calleeExpr + ";", dstFilePath);
        }
        writeLineToDstFile("}", dstFilePath);
        writeLineToDstFile("}", dstFilePath);
        scanner.close();
    }

    /**生成一系列Maini类，主要通过静态字段保存全局变量
     * @throws FileNotFoundException
     */
    public static void genMainiClassFile(String srcFolder, String dstFolder) throws FileNotFoundException {
        String mainClassName = "Main";
        String mainClassField = "_mainClassField.txt";
        String staticDslBlock = "_staticDslBlock.txt";
        for (int i = 0; i <= mainClassIndex; i++) {
            String mainClassFieldFilePath = srcFolder + mainClassName + i + mainClassField;
            String staticDslBlockFieldFilePath = srcFolder + mainClassName + i + staticDslBlock;
            String dstFilePath = null;
            if (DslProgramGen.isGenJavaProgram) {
                dstFilePath = dstFolder + mainClassName + i + ".java";
                // 写入class定义
                writeLineToDstFile("public class " + mainClassName + i + " {", dstFilePath);
            } else {
                dstFilePath = dstFolder + mainClassName + i + ".txt";
                // 写入class定义
                writeLineToDstFile("class " + mainClassName + i + " {", dstFilePath);
            }

            // 读取静态字段并写入文件
            readStaticFieldAndWriteToFile(mainClassFieldFilePath, dstFilePath);
            // 读取静态初始化并写入文件
            readStaticDslBlockAndWriteToFile(staticDslBlockFieldFilePath, dstFilePath);
        }
    }
    public static void main(String[] args) throws FileNotFoundException {
        String srcFolder = "src/test/resources/dslProgramGenForComplexMop/filteredTraces/14/";
        String dstFolder = "src/test/resources/dslProgramGenForComplexMop/filteredTraces/14/classes/";
    }
}
